package com.waimai.mobile.ui.activity;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.waimai.mobile.R;
import com.waimai.mobile.api.WaiMaiServer;
import com.waimai.mobile.api.entity.User;
import com.waimai.mobile.api.model.ResponseModel;
import com.waimai.mobile.ui.WaiMaiApp;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;

import java.nio.charset.Charset;

/**
 * Created by lin on 2015/6/18.
 */

@EActivity(R.layout.activity_user_name)
public class UserNameActivity extends BaseActivity02{

    @App
    public WaiMaiApp app;

    @RestService
    public WaiMaiServer server;

    @ViewById(R.id.userNameEdt)
    public EditText userNameEdt;

    @ViewById(R.id.updateUserNameBtn)
    public Button updateUserNameBtn;

    @AfterInject
    public void afterInject(){
        server.setHeader("Content-Type", "application/json");
    }

    @AfterViews
    public void afterViews(){
        setActionBarTitle("修改用户名");
        userNameEdt.setText(app.getUser().getUserName());
    }

    @Click(R.id.updateUserNameBtn)
    public void updateUserNameBtnClick(View v){
        String userName = userNameEdt.getText().toString();
        int length = userName.getBytes(Charset.forName("GBK")).length;
        if(userName.contains(" ")){
            Toast.makeText(getApplicationContext(), "不允许出现空格", Toast.LENGTH_SHORT).show();
            return ;
        }else if(length < 4 || length > 16){
            Toast.makeText(getApplicationContext(), "限4-16个字符", Toast.LENGTH_SHORT).show();
            return ;
        }
        updateUserNameRequest(userName);
    }

    @Background
    public void updateUserNameRequest(String userName){
        User user = new User();
        user.setUserId(app.getUser().getUserId());
        user.setUserName(userName);
        ResponseModel<User> model = null;
        try{
            model = server.updateUser(user.getUserId(), user);
            if(model != null){
                switch (model.getStatus().getStatusCode()) {
                    case ResponseModel.STATUS_REQUEST_SUCCESS:
                        updateSuccess(model.getData());
                        break;
                    case ResponseModel.STATUS_REGIST_FAILED:
                        updateFailed();
                        break;
                    case ResponseModel.STATUS_SERVER_ERROR:
                        serverError();
                        break;
                }
            }
        }catch (Exception e){
            netError();
        }
    }

    @UiThread
    public void updateSuccess(User user){
        app.setUser(user);
        Toast.makeText(getApplicationContext(), "修改成功", Toast.LENGTH_SHORT).show();
        onBackPressed();
        finish();
    }

    @UiThread
    public void updateFailed(){
        Toast.makeText(getApplicationContext(), "服务器错误，请重试", Toast.LENGTH_SHORT).show();
    }


    @UiThread
    public void serverError(){
        Toast.makeText(getApplicationContext(), "服务器异常，请重试", Toast.LENGTH_SHORT).show();
    }

    @UiThread
    public void netError(){
        Toast.makeText(getApplicationContext(), "网络错误，请重试", Toast.LENGTH_SHORT).show();
    }
}
